package cn.gok.server.service.impl;

import cn.gok.common.dao.customer.CustomerInfoDao;
import cn.gok.common.dao.customer.CustomerLoginDao;
import cn.gok.common.entity.customer.CustomerInfo;
import cn.gok.common.entity.customer.CustomerLogin;
import cn.gok.server.service.CustomerInfoBackstageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote:
 * @date: 2020/11/14
 * @time: 15:51
 */
@Service
@Slf4j
public class CustomerInfoBackstageServiceImpl implements CustomerInfoBackstageService {


    @Autowired
    private CustomerInfoDao customerInfoDao;

    @Autowired
    private CustomerLoginDao customerLoginDao;

    @Override
    public int updateCustomerInfoById(CustomerInfo customerInfo) {
        log.info("顾客信息修改{}", customerInfo);
        return customerInfoDao.updateById(customerInfo);
    }

    @Override
    public int delCustomerInfoByIds(List <Long> ids) {
        log.info("批量删除顾客信息，编号{}", ids);
        return customerInfoDao.deleteBatchIds(ids);
    }

    @Override
    public int delCustomerInfoByIdTrue(List <Long> ids) {
        int size = 0;
        for (Long id : ids) {
            log.info("删除客户信息{}", id);
            size += customerInfoDao.delCustomerInfo(id);
        }
        log.info("成功删除{}条", size);
        return size;
    }

    @Override
    public int insertCustomerInfo(CustomerInfo customerInfo) {
        customerInfo.setCustomerId(System.currentTimeMillis() + "");
        log.info("新增顾客信息{}", customerInfo);
        return customerInfoDao.insert(customerInfo);
    }

    @Override
    public CustomerInfo getCustomerInfoById(Long id) {
        log.info("请求id为{}的客户信息", id);
        return customerInfoDao.selectById(id);
    }

    @Override
    public PageInfo pageInfo(Object target) {
        QueryWrapper <CustomerInfo> wrapper = new QueryWrapper <>();
        wrapper.setEntity((CustomerInfo) target);
        return new PageInfo(customerInfoDao.selectList(wrapper));
    }

    @Override
    public PageInfo pageDel(int page, int limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo(customerInfoDao.pageDel());
    }

    @Override
    public int updateCustomerInfos(String[] ids) {
        List <CustomerInfo> customerInfos = new ArrayList <>();
        for (String id : ids) {
            CustomerInfo customerInfo = new CustomerInfo();
            customerInfo.setGender(null);
            customerInfo.setUserPoint(null);
            customerInfo.setIsDel(1);
            customerInfo.setCustomerLevel(null);
            customerInfo.setCustomerInfId(Long.parseLong(id));
            customerInfos.add(customerInfo);
        }
        int size = 0;
        for (CustomerInfo customerInfo : customerInfos) {
            customerInfoDao.updateById(customerInfo);
            size++;
        }
        return size;
    }


    @Override
    public int delRollBack(Long customerInfId) {
        log.info("已删除的数据还原{}", customerInfId);
        return customerInfoDao.delRollBack(customerInfId);
    }

    @Override
    public int updatePwdByCustomerId(CustomerLogin customerLogin) {
        customerLogin.setPassword(new Md5Hash(customerLogin.getPassword(), null, 999).toString());
        return customerLoginDao.updateById(customerLogin);
    }
}
